// IDE_UCW
// code for bossing UCW around, using the UCW external interface
#include <string>
#include <sstream>
using namespace std;

#include "ide_ucw.h"

int IDE::compile(string file)
{
 string cmd;
 ostringstream ofs(cmd);
 ofs << "#l " << file << ends;
 return m_ucw->exec(ofs.str());
}

int IDE::run(string args)
{
 string cmd;
 ostringstream ofs(cmd);
 ofs << "#r ";
 if (args.size() > 0) ofs << args;
 ofs << ends;
 return m_ucw->exec(ofs.str());
}

void IDE::single_step(bool step_over)
{
    m_ucw->exec(step_over ? "#sto" : "#st");
}

void IDE::stop_program()
{
     m_ucw->exec("#s");
}

void IDE::set_working_dir(string dir)
{ 
   m_ucw->exec("#cd " + dir);    
}

int IDE::set_breakpoint(string file, int& line, bool is_persist)
{
 string cmd, result;
 ostringstream ofs(cmd);
 ofs << (is_persist ? "#b " : "#gt ") << line << " " << file << ends;
 m_ucw->exec_msg(ofs.str(),result); 
 istringstream ins(result);
 string temp, op;
 ins >> temp >> line >> op;
 if (temp == "module") return MODULE_NOT_FOUND;
 if (is_persist) {
    bool success = (op == "set" || op == "unset") && line > 0; 
    return success ? OK : FAIL;
 } else return OK;
}

int IDE::breakpoint_list(string file, int line[], int n)
// has two uses: if called with n = 0, will return the number of
// breakpoints defined in this file.  Otherwise it expects a list
// of lines to set.
{
  string cmd, result;
  ostringstream ofs(cmd);
  int i;
  ofs << "#bs " << file;
  bool was_asking = n == 0;
  if (! was_asking) for(i = 0; i < n; i++) ofs << ' ' << line[i];
  ofs << ends;
  m_ucw->exec_msg(ofs.str(),result);
  istringstream ins(result);
  ins >> n;
  if (was_asking && line != NULL) for(i = 0; i < n; i++) ins >> line[i];
  return n;
}

IDE::IDE(UCW* ucw)
{
   m_ucw = ucw;
}

